package com.yejiali.backend.filter;
import com.yejiali.backend.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import java.util.Map;


/**
 * @Author:yejiali
 * @Date:2025/8/19-08-19-17:06
 * @Decsription:com.yejiali.backend.filter
 * @Version:1.0
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1. 获取请求头中的 token
        String token = request.getHeader("Authorization");
        // 2. 校验 token
        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            //校验成功,放行
            return true;
        } catch (Exception e) {
            // 3. 如果校验失败,响应 401 状态码
            response.setStatus(401);
            return false;
        }
    }
}
